﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Configuration.Provider;


namespace Commerce.Providers
{
    class FlatRateShippingProvider : FulfillmentProvider
    {
        string _connectionString = "";
        string _connectionStringName = "";

        #region Provider overrides
        public override void Initialize(string name, NameValueCollection config)
        {
            // Verify that config isn't null
            if (config == null)
                throw new ArgumentNullException("config");

            base.Initialize(name, config);

            _connectionStringName = config["connectionStringName"].ToString();

            if (String.IsNullOrEmpty(_connectionStringName))
                throw new ProviderException("Empty or missing connectionStringName");

            config.Remove("connectionStringName");

            if (WebConfigurationManager.ConnectionStrings[_connectionStringName] == null)
                throw new ProviderException("Missing connection string");

            _connectionString = WebConfigurationManager.ConnectionStrings[_connectionStringName].ConnectionString;

            if (String.IsNullOrEmpty(_connectionString))
                throw new ProviderException("Empty connection string");

        }
        #endregion

        public override DeliveryOptionCollection GetDeliveryOptions(PackageInfo package)
        {
            IDataReader rdr = Commerce.Common.ShippingRate.FetchAll();

            DeliveryOptionCollection coll = new DeliveryOptionCollection();
            coll.Load(rdr);
            rdr.Close();
            return coll;
        }
        public override DeliveryOptionCollection GetDeliveryOptions(PackageInfo package, DeliveryRestrictions restrictions)
        {
            string columnName = "";
            bool val = false;

            if (restrictions == DeliveryRestrictions.Air)
            {
                columnName = "isAirOnly";
                val = false;
            }
            else if (restrictions == DeliveryRestrictions.Freight || restrictions == DeliveryRestrictions.Ground)
            {
                columnName = "isGroundOnly";
                val = false;
            }

            IDataReader rdr;
            if (val)
            {
                rdr = Commerce.Common.ShippingRate.FetchByParameter(columnName, val);
            }
            else
            {
                rdr = Commerce.Common.ShippingRate.FetchAll();
            }

            DeliveryOptionCollection coll = new DeliveryOptionCollection();
            coll.Load(rdr);
            rdr.Close();
            return coll;
        }

        public override DeliveryOptionCollection GetDeliveryOptions(PackageInfo package, decimal orderAmount, int orderQuantity)
        {
            IDataReader rdr = Commerce.Common.ShippingRate.FetchAll();

            DeliveryOptionCollection coll = new DeliveryOptionCollection();

            coll.Load(rdr);
            rdr.Close();

            // This is to allow Free Shipping on Ground Shipping over 50.00
            foreach (DeliveryOption ship in coll)
            {
                if( ship.AmountPerUnit > Convert.ToDecimal(0.0) )
                {
                    ship.Rate = package.Weight * ship.AmountPerUnit;
                }
            }

            return coll;
        }
    }
}
